package org.example.edusmart.controller.Teacher;
import jakarta.servlet.http.HttpServletRequest;
import org.example.edusmart.dao.WorkDao;
import org.example.edusmart.pojo.CourseDirectoryPojo;
import org.example.edusmart.pojo.CoursePojo;
import org.example.edusmart.pojo.UserPojo;
import org.example.edusmart.request.TeaOrStu.*;
import org.example.edusmart.service.CourseService;
import org.example.edusmart.service.UserService;
import org.example.edusmart.utils.AliOSSUtil;
import org.example.edusmart.utils.GetTokenInfo;
import org.example.edusmart.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * 教师进行课程管理
 */
@RestController
@RequestMapping("/TeacherCourse")
public class TeaCourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private UserService userService;

    @Autowired
    private GetTokenInfo getTokenInfo;

    @Autowired
    private AliOSSUtil aliOSSUtil;


    /**
     * 添加课程
     */
    @PostMapping("/create/")
    @PreAuthorize("hasAuthority('teacher_permission')")
    public Result createCourse(HttpServletRequest request,@RequestBody CoursePojo course) {
        //解析token获取用户id
        String Id = getTokenInfo.getUserIdByJwt(request);
        //获取用户信息
        UserPojo user = userService.getUserByUserId(Id);
        String courseTeacher = user.getUsername();
        //将String类型的数字字符串转换为int类型
        int userId = Integer.parseInt(Id);
        //获取本地时间
        LocalDateTime createTime=LocalDateTime.now();
        course.setCreateTime(createTime);
        course.setCourseTeacher(courseTeacher);
        int result = courseService.addCourse(course);
        if (result>0) {
            // 获取课程id
            int courseId = course.getCourseId(course);
            courseService.insertCourseIntoTeacher(userId,courseId);
            return new Result(course,200,"创建课程成功");
        }else {
            return new Result(400,"创建课程失败");
        }
    }

    /**
     *上传课程资料
     */
    @PostMapping("/uploadDirectory/")
    @PreAuthorize("hasAuthority('teacher_permission')")
    public Result uploadCourseDirectory(MultipartFile file, @RequestParam String courseId) {
        if (file.isEmpty()) {
            return new Result(404,"文件不能为空");
        }
        try {
            String url = aliOSSUtil.upload(file);
            CourseDirectoryPojo courseDirectory = new CourseDirectoryPojo();
            String courseDirectoryName = file.getOriginalFilename();
            courseDirectory.setCourseId(Integer.parseInt(courseId));
            courseDirectory.setCourseDirectoryName(courseDirectoryName);
            courseDirectory.setCourseDirectoryUrl(url);
            int result = courseService.uploadCourseDirectory(courseDirectory);

            if (result > 0) {
                return new Result(url,200,"课程资料上传成功");
            } else {
                return new Result(404,"课程资料上传失败");
            }
        } catch (IOException e) {
            return new Result(404,"课程资料上传失败");
        }
    }

    /**
     * 拉学生进课程
     */
    @PostMapping("/invite/")
    @PreAuthorize("hasAuthority('teacher_permission')")
    public Result inviteStu(HttpServletRequest request, @RequestBody StudentInfoCoursePojo studentInfoCourse){
        int userId = Integer.parseInt(getTokenInfo.getUserIdByJwt(request));
        int courseId = studentInfoCourse.getCourseId();
        Integer result = courseService.verifyTeaCourse(courseId, userId);
        if (result != null) {
            String  number = studentInfoCourse.getNumber();
            int stuId = courseService.getUserIdByNumber(number);
            courseService.inviteStudent(courseId, stuId);
            return new Result(200,"拉取学生成功");
        } else {
            return new Result(404, "你还没有课程操作的权限");
        }
    }

    /**
     * 课程给分
     */
    @PostMapping("/score/")
    @PreAuthorize("hasAuthority('teacher_permission')")
    public Result scoreCourse(@RequestBody CourseGradePOjo courseGrade){
        courseService.scoreCourse(courseGrade);
        return new Result(200,"评分课程成功");
    }

    /**
     * 查看课程所有学生
     */
    @PostMapping("/getAllStu/")
    @PreAuthorize("hasAnyAuthority('teacher_permission','student_peimission')")
    public Result getAllStu(@RequestBody CourseIdPojo course){
        int courseId = course.getCourseId();
//        int userId = courseService.getTeacher(courseId);
        List<HashMap<String, Object>> data =  courseService.getAllStu(courseId);
        return new Result(data,200,"获取学生列表成功");
    }



}
